package cn.edu.jnu.experiment1.controller;

import cn.edu.jnu.experiment1.common.Page;
import cn.edu.jnu.experiment1.entity.User;
import cn.edu.jnu.experiment1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> list() {
        return userService.findAll();
    }

    @GetMapping("/id/{id}")
    public User get(@PathVariable Integer id) {
        return userService.findById(id);
    }

    @GetMapping("/desc")
    public List<User> findByDesc() {
        return userService.findByOrder();
    }

    @GetMapping("/name/{name}")
    public List<User> findByName(@PathVariable String name) {
        return userService.findByUsername(name);
    }

    @GetMapping("/more")
    public List<User> findMore(@RequestParam String username, @RequestParam String name){
        return userService.findByMore(username, name);
    }

    @GetMapping("/like")
    public List<User> findLike(@RequestParam String username, @RequestParam String name) {
        return userService.findByLike(username, name);
    }

    @GetMapping("/page")
    public Page<User> findByPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam String username,
                                 @RequestParam String name) {
        return userService.findByPage(pageNum, pageSize, username, name);
    }

    @PostMapping
    public String create(@RequestBody User user) {
        userService.insert(user);
        return "添加成功";
    }

    // 修改用户信息
    @PutMapping
    public String update(@RequestBody User user) {
        userService.update(user);
        return "修改成功";
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id) {
        userService.delete(id);
        return "删除成功";
    }
    @DeleteMapping("/all")
    public String deleteAll(@RequestBody List<Integer> ids) {
        userService.deleteAll(ids);
        return "全部删除成功";
    }
}

// Post可以添加新用户
    /* postman格式如下：
   {
  "username": "tom",
  "password": "123456",
  "name": "汤姆",
  "phone": "12345678900",
  "email": "tom@example.com",
  "address": "广州大学城",
  "avatar": "https://example.com/avatar.jpg"
    }*/